package com.lzk.controller;

import com.lzk.domain.ResponseResult;
import com.lzk.domain.entity.Role;
import com.lzk.domain.entity.User;
import com.lzk.domain.vo.PageVo;
import com.lzk.domain.vo.UserInfoAndRoleIdsVo;
import com.lzk.service.RoleService;
import com.lzk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * author: 刘中魁
 */
@RestController
@RequestMapping("/system/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public ResponseResult getUserList(User user,Integer pageNum,Integer pageSize){
        PageVo pageVo =  userService.getUserList(user,pageNum,pageSize);
        return ResponseResult.okResult(pageVo);
    }

    @DeleteMapping("/{id}")
    public ResponseResult delectUser(@PathVariable Long id){
        userService.removeById(id);

        return ResponseResult.okResult();
    }

    @PostMapping
    public ResponseResult addUser(@RequestBody User user){
        userService.addUser(user);
        return ResponseResult.okResult();
    }
    //-----------------------修改用户-①根据id查询用户信息-----------------------------

    @Autowired
    private RoleService roleService;

    @GetMapping(value = { "/{userId}" })
    public ResponseResult getUserInfoAndRoleIds(@PathVariable(value = "userId") Long userId) {
        List<Role> roles = roleService.listAllRole();
        User user = userService.getById(userId);
        //当前用户所具有的角色id列表
        List<Long> roleIds = roleService.selectRoleIdByUserId(userId);

        UserInfoAndRoleIdsVo vo = new UserInfoAndRoleIdsVo(roleIds,roles,user);
        return ResponseResult.okResult(vo);
    }

    @PutMapping
    public ResponseResult updateUser(@RequestBody User user){
        this.userService.updateUsserInfo(user);
        return ResponseResult.okResult();
    }

}
